package com.pudu.mydemo.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.pudu.mydemo.adapter.MapAdapter;
import com.pudu.mydemo.fragment.MapFragment;
import com.pudutech.pdrobot.R;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by guiliu on 2016/7/13.
 */
public class RouteSelectActivity extends BaseHomeActivity{
    private ViewPager map_viewpager;
    private List<Fragment> fragments;
    private ImageView left,right,map,ok;
    private  MapAdapter mapAdapter;
    private ListView map_list;
    private static  int position =0;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.route_selecter;
    }

    @Override
    public void initView() {
        fragments = new ArrayList<Fragment>();
        map_viewpager = (ViewPager)findViewById(R.id.map_viewpager);
        left = (ImageView)findViewById(R.id.left);
        right = (ImageView)findViewById(R.id.right);
        map = (ImageView)findViewById(R.id.map);
        map_list=(ListView)findViewById(R.id.map_list);
        ok = (ImageView)findViewById(R.id.ok);
    }

    @Override
    public void initData() {
        fragments.add(new MapFragment());
        //fragments.add(new MapFragment2());
        //fragments.add(new MapFragment3());
    }


    @Override
    protected void onResume() {
        super.onResume();
        map.setVisibility(View.VISIBLE);
        map_list.setVisibility(View.GONE);
        mapAdapter = new MapAdapter(getSupportFragmentManager(), fragments);
        map_viewpager.setAdapter(mapAdapter);
        map_viewpager.setOnPageChangeListener(new TabOnPageChangeListener());

        map_list.setAdapter(new ArrayAdapter<String>(this, R.layout.map_list,R.id.map_item,getData()));
    }


    @Override
    public void initEvent() {
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //position--;
                //map_viewpager.setCurrentItem(position<0?mapAdapter.getCount()-1:position);
                try {
                    MapFragment mapfragment = (MapFragment) mapAdapter.getItem(0);
                    mapfragment.showNextMLines();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
        });

        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // position++;
               // map_viewpager.setCurrentItem(position>=mapAdapter.getCount()?0:position);
                try {
                    MapFragment mapfragment = (MapFragment) mapAdapter.getItem(0);
                    mapfragment.showPreviousMLines();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
        });

        map.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                map.setVisibility(View.GONE);
                if(View.VISIBLE == map_list.getVisibility()) {
                    map_list.setVisibility(View.GONE);
                } else {
                    map_list.setVisibility(View.VISIBLE);
                }
            }
        });

        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int position = 0;
                try {
                    MapFragment mapfragment = (MapFragment) mapAdapter.getItem(0);
                    position = mapfragment.getCurrentMapLines();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }

//                Toast.makeText(getApplicationContext(),"地图"+position,Toast.LENGTH_LONG).show();
                Intent intent = new Intent(RouteSelectActivity.this, SmartActivity.class);
                intent.putExtra("position",position);
                setResult(0,intent);
                finish();
            }
        });
    }


    public class TabOnPageChangeListener implements OnPageChangeListener {

        //当滑动状态改变时调用
        public void onPageScrollStateChanged(int state) {

        }

        //当前页面被滑动时调用
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels){
           /* LinearLayout.LayoutParams lp=(android.widget.LinearLayout.LayoutParams) mTabLine.getLayoutParams();
            //返回组件距离左侧组件的距离
            lp.leftMargin= (int) ((positionOffset+position)*screenWidth/3);
            mTabLine.setLayoutParams(lp);*/
        }

        //当新的页面被选中时调用
        public void onPageSelected(int position) {
            RouteSelectActivity.position = position;
        }
    }

    private List<String> getData(){

        List<String> data = new ArrayList<String>();
        data.add("地图1");
        /*data.add("地图2");
        data.add("地图3");
        data.add("地图4");*/

        return data;
    }




}
